import {defineStore} from "pinia";
import {getLoginCode, login} from "@/api/login";
import {removeLocalStore, setLocalStore} from "@/utils/user.ts";

const useDialogStore = defineStore("dialog", {
    state: () => ({
        isShow: false,
        code: "",
        userInfo: JSON.parse(localStorage.getItem("userInfo") as string) ?? {},
    }),
    actions: {
        async getLoginCodeData(phone: string) {
            const res: any = await getLoginCode(phone)
            this.code = res.data
        },
        async loginData(data: any) {
            const res: any = await login(data)
            this.userInfo = res.data
            //本地存储用户信息
            setLocalStore("userInfo", this.userInfo)
        },
        loginOut() {
            //清楚用户数据
            this.userInfo = {}
            removeLocalStore()
        },
    }
});


export default useDialogStore
